<?php
/**
 * 管理员控制器
 */
namespace Admin\Controller;
use Think\Controller;
class AdminController extends InitController {
    
	// 添加后台用户
	public function register() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//权限
		$role = M('Role')->where(array('status'=>1))->order('id asc')->select();
		$this->assign('role',$role);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Admin/register');
	}
	
	// 添加后台用户操作
	public function do_register() {
		$username = I('username','','strip_tags,htmlspecialchars,trim');
		$password = I('password','','strip_tags,htmlspecialchars,trim');
		$email = I('email','','strip_tags,htmlspecialchars,trim');
		$nickname = I('nickname','','strip_tags,htmlspecialchars,trim');
		$role = I('role',0,'intval');
		
		if (empty($username)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'用户名不能为空！','field'=>'username'));
		}
		
		if (!is_username($username)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'用户名只能3-20位英文数字组成！','field'=>'username'));
		}
		
		if (empty($password)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'用户密码不能为空！','field'=>'password'));
		}
		
		if (!is_password($password)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'密码必须6位以上20位以下字符组成！','field'=>'password'));
		}
		
		if (!empty($email) && !isEmail($email)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱格式不正确！','field'=>'email'));
		}
		
		if (!$role) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请选择角色','field'=>'password'));
		}
		
		$adminModel = D('Admin');
		
		// 用户名是否已被使用
		if (!$adminModel->checkUsername($username)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'用户名已被注册，请换一个再试！','field'=>'username'));
		}
		
		// 邮箱是否已被使用
		if (!empty($email) && !$adminModel->checkEmail($email)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱已被注册，请换一个再试！','field'=>'email'));
		}
		
		$salt = random(8);
		
		$data['username'] = $username;
		$data['password'] = md5($password . $salt);
		$data['salt'] = $salt;
		$data['createtime'] = NOW_TIME;
		$data['role'] = $role;
		$data['status'] = 1;
		$data['email'] = $email;
		$data['nickname'] = $nickname;
		
		if ($adminModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
		
	}
	
	// 编辑用户
	public function userEdit() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$id = I('uid',0,'intval');
		if (!$id) {
			$this->error('非法操作！');
		}
		
		$adminModel = D('Admin');
		$userinfo = $adminModel->getUserInfoById($id);
		
		if(empty($userinfo)){
			$this->error('用户不存在或已被删除！');
		}
		
			//权限
		$role = M('Role')->where(array('status'=>1))->order('id asc')->select();
		$this->assign('role',$role);
		
		$this->assign('userinfo', $userinfo);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Admin/userEdit');
	}
	
	// 编辑用户信息操作
	public function do_userEdit() {
		$id = I('uid',0,'intval');
		
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		
		$password = I('password','','strip_tags,htmlspecialchars,trim');
		$email = I('email','','strip_tags,htmlspecialchars,trim');
		$nickname = I('nickname','','strip_tags,htmlspecialchars,trim');
		$role = I('role',2,'intval');
		
		if (!empty($email) && !isEmail($email)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱格式不正确！','field'=>'email'));
		}
		
		if (!empty($password)) {
			if (!is_password($password)) {
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'密码必须6位以上20位以下字符组成！','field'=>'password'));
			}
		}
		
		$adminModel = D('Admin');
		
		// 邮箱是否已被使用
		if (!empty($email) && !$adminModel->checkEmail($email,array('id'=>array('neq',$id)))) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱已被注册，请换一个再试！','field'=>'email'));
		}
		
		if ($password) {
			$salt = random(8);
			$data['password'] = md5($password . $salt);
			$data['salt'] = $salt;
		}
		$data['role'] = $role;
		$data['email'] = $email;
		$data['nickname'] = $nickname;
		
		if ($adminModel->where(array('id'=>$id))->save($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 后台用户列表
	public function userList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$adminModel = D('Admin');
		$where = array();

		$username = I('username');
		$nickname = I('nickname');
		$email = I('email');
		$start_time = strtotime(I('start_time'));
		$end_time = strtotime(I('end_time'));
		$status = I('status');
		$where['a.status'] = array('NEQ','-1');
		if($status !== '' && $status != 'All'){
			$status = $status + 0;
			$where['a.status'] = $status;
		}
		if($username){
			$where['a.username'] = array("like",'%'.$username.'%');
		}
		if($nickname){
			$where['a.nickname'] = array("like",'%'.$nickname.'%');
		}
		if($email){
			$where['a.email'] = array("like",'%'.$email.'%');
		}
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['a.createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间 
				$where['a.createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间 
				$where['a.createtime'] = array('ELT', $end_time);
			}
		}
	
		$adminModel = D('Admin');
		$total = $adminModel->alias('a')->where($where)->count();
		
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		
		$list = $adminModel->alias('a')->field('a.*,r.name')
						->join('left join __ROLE__ as r on a.role = r.id')
						->where($where)
						->order('a.id desc')
						->limit($Page->firstRow.','.$Page->listRows)
						->field('a.*,r.name,r.right')
						->select();
		
		
		$this->assign('list',$list);
		$this->assign('pager',$show);

		$this->assign('status',$status);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('list',$list);
		$this->assign('username',$username);
		$this->assign('nickname',$nickname);
		$this->assign('email',$email);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->display('Admin/userList');
	}
	
	// 锁定/解锁用户
	public function userLock() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$status = I('status',0,'intval');
		$id = I('uid',0,'intval');
		if(!$id){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (session('uid') == $id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败，不允许修改自己的状态！','field'=>''));
		}
		$adminModel = D('Admin');
		$userStatus = $adminModel->getUserStatus($id);
		if($status == $userStatus){
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		}
		if ($adminModel->changeStatus($status,$id)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		}else{
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 删除用户
	public function userDel() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (session('uid') == $id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败，不允许删除自己！','field'=>''));
		}
		if (D('Admin')->delUser($id)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 修改资料
	public function changeInfo() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Admin/changeInfo');
	}
	
	// 修改资料操作
	public function do_changeInfo() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = session('uid');
		
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		
		$email = I('email','','strip_tags,htmlspecialchars,trim');
		$nickname = I('nickname','','strip_tags,htmlspecialchars,trim');
		if (!empty($email) && !isEmail($email)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱格式不正确！','field'=>'email'));
		}
		
		$adminModel = D('Admin');
		
		// 邮箱是否已被使用
		if (!empty($email) && !$adminModel->checkEmail($email,array('id'=>array('neq',$id)))) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'电子邮箱已被注册，请换一个再试！','field'=>'email'));
		}
		
		$data['email'] = $email;
		$data['nickname'] = $nickname;
		
		if ($adminModel->where(array('id'=>$id))->save($data)) {
			session('email',$email);
			session('nickname',$nickname);
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 修改密码页面
	public function changePass() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Admin/changePass');
	}
	
	// 修改密码操作
	public function do_changePass() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$oldpass = I('oldpass','','strip_tags,htmlspecialchars,trim');
		$password = I('password','','strip_tags,htmlspecialchars,trim');
		$id = session('uid') + 0;
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (!is_password($password)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'新密码必须6位以上20位以下字符组成！','field'=>'password'));
		}
		$adminModel = D('Admin');
		
		$userInfo = $adminModel->getUserInfoById($id);

		// 密码校验
		if (md5($oldpass . $userInfo['salt']) != $userInfo['password']) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'原密码错误！','field'=>''));
		}
		
		$salt = random(8);
		$data['password'] = md5($password . $salt);
		$data['salt'] = $salt;
		
		if ($adminModel->where(array('id'=>$id))->save($data)) {
			A('Logout')->logout();
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
}